package com.mmloo.manage.controller;

import com.mmloo.common.message.Message;
import com.mmloo.common.model.*;
import com.mmloo.common.page.Pager;
import com.mmloo.manage.service.PromotionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by jessy on 2015/10/6.
 * 商品促销模块管理
 */
@Controller
@RequestMapping("/manage/goods/promotion")
public class PromotionManageController {
    @Autowired
    PromotionService<GroupBuy> groupBuyPromotionService;

    @Autowired
    PromotionService<GivingBuy> givingBuyPromotionService;

    @Autowired
    PromotionService<FastBuy> fastBuyPromotionService;

    @Autowired
    PromotionService<CombinationBuy> combinationBuyPromotionService;

    @Autowired
    PromotionService<ReturnBuy> returnBuyPromotionService;

    /**
     * 请求添加团购商品页面
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/groupBuy/add",method = RequestMethod.GET)
    public String addGroupBuy(Model model) throws Exception {
        model.addAttribute("groupBuy", new GroupBuy());
        return "manage/promotion/groupBuy/add";
    }

    /**
     * 添加团购商品
     * @param groupBuy
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/groupBuy/add",method = RequestMethod.POST)
    public String addGroupBuy(GroupBuy groupBuy,Model model) throws Exception {
        groupBuyPromotionService.add(groupBuy);
        model.addAttribute("message",new Message(true,"添加团购数据成功!"));

        return "redirect:/manage/goods/promotion/groupBuy/list";
    }


    /**
     * 分页显示团购所有数据
     * @param request
     * @param response
     * @param keyword
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/groupBuy/list",method = RequestMethod.GET)
    public String listGroupBuy(HttpServletRequest request,HttpServletResponse response,String keyword,Model model) throws Exception {
        Pager<GroupBuy> groupBuyPager = groupBuyPromotionService.getByPager(new Pager<GroupBuy>(request, response), keyword,GroupBuy.class);
        model.addAttribute("pager",groupBuyPager);
        if(groupBuyPager.getList().size()!=0) {
            model.addAttribute("message", new Message(true, "成功获取所有商品"));
        }else {
            model.addAttribute("message", new Message(true, "没有此类商品"));
        }
        return "manage/promotion/groupBuy/list";
    }

    /**
     * 请求满几赠几商品页面
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/givingBuy/add",method = RequestMethod.GET)
    public String addGivingBuy(Model model) throws Exception {
        model.addAttribute("givingBuy", new GivingBuy());
        return "manage/promotion/givingBuy/add";
    }

    /**
     * 添加满几赠几商品
     * @param givingBuy
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/givingBuy/add",method = RequestMethod.POST)
    public String addGivingBuy(GivingBuy givingBuy,Model model) throws Exception {
        givingBuyPromotionService.add(givingBuy);
        model.addAttribute("message",new Message(true,"添加满几赠几商品数据成功！"));
        return "redirect:/manage/goods/promotion/givingBuy/list";
    }

    /**
     * 满几赠几商品分页显示
     * @param request
     * @param response
     * @param keyword
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/givingBuy/list",method = RequestMethod.GET)
    public String listGivingBuy(HttpServletRequest request,HttpServletResponse response,String keyword,Model model) throws Exception {
        Pager<GivingBuy> givingBuyPager = givingBuyPromotionService.getByPager(new Pager<GivingBuy>(request, response), keyword,GivingBuy.class);
        model.addAttribute("pager",givingBuyPager);
        if(givingBuyPager.getList().size()!=0) {
            model.addAttribute("message", new Message(true, "成功获取所有商品"));
        }else {
            model.addAttribute("message", new Message(true, "没有此类商品"));
        }
        return "manage/promotion/givingBuy/list";
    }

    /**
     * 请求添加限时抢购商品页面
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/fastBuy/add",method = RequestMethod.GET)
    public String addFastBuy(Model model) throws Exception {
        model.addAttribute("fastBuy", new FastBuy());
        return "manage/promotion/fastBuy/add";
    }

    /**
     * 添加限时抢购商品
     * @param fastBuy
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/fastBuy/add",method = RequestMethod.POST)
    public String addFastBuy(FastBuy fastBuy,Model model) throws Exception {
        fastBuyPromotionService.add(fastBuy);
        model.addAttribute("message",new Message(true,"添加秒杀限时抢购商品数据成功！"));
        return "redirect:/manage/goods/promotion/fastBuy/list";
    }

    /**
     * 抢购商品分页列表
     * @param request
     * @param response
     * @param keyword
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/fastBuy/list",method = RequestMethod.GET)
    public String listFastBuy(HttpServletRequest request,HttpServletResponse response,String keyword,Model model) throws Exception {
        Pager<FastBuy> fastBuyPager = fastBuyPromotionService.getByPager(new Pager<FastBuy>(request, response), keyword,FastBuy.class);
        model.addAttribute("pager",fastBuyPager);
        if(fastBuyPager.getList().size()!=0) {
            model.addAttribute("message", new Message(true, "成功获取所有商品"));
        }else {
            model.addAttribute("message", new Message(true, "没有此类商品"));
        }
        return "manage/promotion/fastBuy/list";
    }

    /**
     * 请求添加组合商品管理页面c
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/combinationBuy/add",method = RequestMethod.GET)
    public String addCombinationBuy(Model model) throws Exception {
        model.addAttribute("combinationBuy", new CombinationBuy());
        return "manage/promotion/combinationBuy/add";
    }
    /**
     * 添加组合商品管理
     * @param combinationBuy
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/combinationBuy/add",method = RequestMethod.POST)
    public String addCombinationBuy(CombinationBuy combinationBuy,Model model) throws Exception {
        combinationBuyPromotionService.add(combinationBuy);
        model.addAttribute("message",new Message(true,"添加组合套餐商品数据成功！"));
        return "redirect:/manage/goods/promotion/combinationBuy/list";
    }

    /**
     * 组合套餐商品分页列表
     * @param request
     * @param response
     * @param keyword
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/combinationBuy/list",method = RequestMethod.GET)
    public String listCombinationBuy(HttpServletRequest request,HttpServletResponse response,String keyword,Model model) throws Exception {
        Pager<CombinationBuy> combinationBuyPager = combinationBuyPromotionService.getByPager(new Pager<CombinationBuy>(request, response), keyword,CombinationBuy.class);
        model.addAttribute("pager",combinationBuyPager);
        if(combinationBuyPager.getList().size()!=0) {
            model.addAttribute("message", new Message(true, "成功获取所有商品"));
        }else {
            model.addAttribute("message", new Message(true, "没有此类商品"));
        }
        return "manage/promotion/combinationBuy/list";
    }

    /**
     * 请求添加购物返还商品页面
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/returnBuy/add",method = RequestMethod.GET)
    public String addReturnBuy(Model model) throws Exception {
        model.addAttribute("returnBuy", new ReturnBuy());
        return "manage/promotion/returnBuy/add";
    }
    /**
     * 添加购物返还商品
     * @param returnBuy
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/returnBuy/add",method = RequestMethod.POST)
    public String addReturnBuy(ReturnBuy returnBuy,Model model) throws Exception {
        returnBuyPromotionService.add(returnBuy);
        model.addAttribute("message", new Message(true, "添加购物返还商品数据成功！"));
        return "redirect:/manage/goods/promotion/returnBuy/list";
    }


    /**
     * 购物返还商品的分页列表
     * @param request
     * @param response
     * @param keyword
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/returnBuy/list",method = RequestMethod.GET)
    public String listReturnBuy(HttpServletRequest request,HttpServletResponse response,String keyword,Model model) throws Exception {
        Pager<ReturnBuy> returnBuyPager = returnBuyPromotionService.getByPager(new Pager<ReturnBuy>(request, response), keyword,ReturnBuy.class);
        model.addAttribute("pager",returnBuyPager);
        if(returnBuyPager.getList().size()!=0) {
            model.addAttribute("message", new Message(true, "成功获取所有商品"));
        }else {
            model.addAttribute("message", new Message(true, "没有此类商品"));
        }
        return "manage/promotion/returnBuy/list";
    }

    @ResponseBody
    @RequestMapping(value = "/add/json",method = RequestMethod.POST)
    public String addGroupBuy(String select){
        System.out.println("=======>" + select);
        return new Message(true,"",select).toJson();
    }
}
